package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.exception.ExceptionCast;
import com.itheima.common.response.ResponseResult;
import com.itheima.common.response.StatusCode;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dqh
 * @create 2020-09-18 19:08
 * @desc 用户业务层实现类
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @desc: 查询所有用户的方法
     * @return:
     * @author: Dqh  2020/9/18  19:07
     */
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    /**
     * @param user
     * @desc: 添加用户
     * @return:
     * @author: Dqh  2020/9/18  19:43
     */
    @Override
    public boolean addUser(User user) {
        if(user.getUserName() == null){
            ExceptionCast.cast(ResponseResult.errorResult(StatusCode.APUSER_USERNAMEISNULL_ERROR));
        }
        return userMapper.addUser(user);
    }

    /**
     * @param uuid
     * @desc: 根据uuid删除用户
     * @return:
     * @author: Dqh  2020/9/18  19:56
     */
    @Override
    public boolean deleteUser(Integer uuid) {
        return userMapper.deleteUser(uuid);
    }

    /**
     * @param uuid
     * @desc: 查询单个用户信息
     * @return:
     * @author: Dqh  2020/9/18  20:09
     */
    @Override
    public User findOne(Integer uuid) {
        return userMapper.findOne(uuid);
    }

    /**
     * @param user
     * @desc: 修改用户
     * @return:
     * @author: Dqh  2020/9/18  20:09
     */
    @Override
    public boolean update(User user) {
        return userMapper.update(user);
    }

    /**
     * @param user
     * @param page
     * @param size
     * @desc: 分页查询
     * @return:
     * @author: Dqh  2020/9/18  20:35
     */
    @Override
    public Page<User> search(User user, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<User> pageList =
                (Page<User>) userMapper.search(user);
        System.out.println("pageList = " + pageList);
        return pageList;
    }




}
